package cn.dong.ucenter.service.impl;

import cn.dong.api.utils.JwtInfo;
import cn.dong.api.utils.JwtUtils;
import cn.dong.api.utils.MD5;
import cn.dong.base.dto.MemberDto;
import cn.dong.base.dto.TradeOrderDto;
import cn.dong.base.handler.CustomException;
import cn.dong.ucenter.domain.Member;
import cn.dong.ucenter.domain.dto.LoginVo;
import cn.dong.ucenter.domain.dto.MemberBasicVo;
import cn.dong.ucenter.domain.dto.MemberRegisterVo;
import cn.dong.ucenter.feign.TradeOrderService;
import cn.dong.ucenter.mapper.MemberMapper;
import cn.dong.ucenter.service.MemberService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.netflix.client.ClientException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author You-Dong
 * @since 2021-02-18
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private TradeOrderService tradeOrderService;

    @Autowired
    private MemberMapper memberMapper;


    /**
     * 注册
     *
     * @param memberRegisterVo
     */
    @Override
    public void register(MemberRegisterVo memberRegisterVo) throws ClientException {

        // 非空判断
        if (StringUtils.isEmpty(memberRegisterVo.getCode())) {
            throw new CustomException(40000, "验证码不能为空！");
        }

        if (StringUtils.isEmpty(memberRegisterVo.getMobile())) {
            throw new CustomException(40000, "手机号不能为空！");
        }

        if (StringUtils.isEmpty(memberRegisterVo.getNickname())) {
            throw new CustomException(40000, "用户名不能为空！");
        }

        if (StringUtils.isEmpty(memberRegisterVo.getPassword())) {
            throw new CustomException(40000, "密码不能为空！");
        }

        // 验证验证码
        String checkCode = (String) redisTemplate.opsForValue().get(memberRegisterVo.getMobile());
        if (!memberRegisterVo.getCode().equals(checkCode)) {
            throw new CustomException(40000, "验证码输入有误！");
        }

        // 判断该用户是否存在
        QueryWrapper<Member> queryWrapper = new QueryWrapper<Member>();
        queryWrapper.eq("mobile", memberRegisterVo.getMobile());
        Integer i = super.baseMapper.selectCount(queryWrapper);
        if (i > 0) {
            throw new CustomException(40000, "该手机号已经注册！");
        }

        // 从vo中提取注册内容
        Member member = new Member();
        // 自定义默认头像
        member.setAvatar("https://online-education-teachers.oss-cn-beijing.aliyuncs.com/member/avatar-boy.gif?versionId=CAEQDRiBgICdgPH6vRciIDU3NDU4OWRmN2U0NDQyOTY5OWQ5NjNlNTE3OGE3NzRl");
        // 加密密码
        member.setPassword(MD5.encrypt(memberRegisterVo.getPassword()));
        BeanUtils.copyProperties(memberRegisterVo, member);
        // 加入数据库
        super.baseMapper.insert(member);
    }

    /**
     * 登录
     *
     * @param loginVo
     * @return
     */
    @Override
    public String login(LoginVo loginVo) {
        // 判断参数
        if (StringUtils.isEmpty(loginVo)
                || StringUtils.isEmpty(loginVo.getMobile())
                || StringUtils.isEmpty(loginVo.getPassword())) {
            throw new CustomException(40000, "参数不合法");
        }
        // 判断手机号是否存在
        QueryWrapper<Member> queryWrapper = new QueryWrapper<Member>();
        queryWrapper.eq("mobile", loginVo.getMobile());
        Member member = super.baseMapper.selectOne(queryWrapper);
        if (member == null) {
            throw new CustomException(40000, "手机号不存在！");
        }
        if (!member.getMobile().equals(loginVo.getMobile())) {
            throw new CustomException(40000, "手机号不存在！");
        }

        // 判断密码是否有误
        if (!member.getPassword().equals(loginVo.getPassword())) {
            throw new CustomException(40000, "手机号或密码有误！");
        }

        // 判断用户是否被封
        if (member.getDisabled()) {
            throw new CustomException(40000, "该账户已被永久封禁！！");
        }
        // 增加上一次的登录时间
        member.setGmtLogin(new Date());
        // 修改
        super.baseMapper.updateById(member);
        // 生成token并返回
        JwtInfo info = new JwtInfo();
        info.setId(member.getId());
        info.setAvatar(member.getAvatar());
        info.setNickname(member.getNickname());
        return JwtUtils.getJwtToken(info, 1800);
    }

    /**
     * 根据会员id，查询会员信息，用于微服务调用
     *
     * @param id
     * @return
     */
    @Override
    public MemberDto getMemberById(String id) {
        Member member = super.baseMapper.selectById(id);
        // 复制Bean
        MemberDto memberDto = new MemberDto();
        BeanUtils.copyProperties(member, memberDto);
        return memberDto;
    }

    /**
     * 查询用户的基本资料
     *
     * @param id
     * @return
     */
    @Override
    public MemberBasicVo getBasicInfo(String id) {
        MemberBasicVo member = baseMapper.getBasicInfo(id);
        return member;
    }

    /**
     * 查询这个用户的订单
     *
     * @param id
     * @return
     */
    @Override
    public List<TradeOrderDto> getOrderAll(String id) {
        List<TradeOrderDto> result = tradeOrderService.getOrderByMemberId(id);
        return result;
    }

    /**
     * 获取今天的注册数量
     *
     * @param day
     * @return
     */
    @Override
    public Integer getRegisterCountByDay(String day) {
        return memberMapper.getRegisterCountByDay(day);
    }

    /**
     * 获取今天的登录数量
     *
     * @param day
     * @return
     */
    @Override
    public Integer getLoginCountByDay(String day){
        return memberMapper.getLoginCountByDay(day);
    }

}
